$(function () {
    if (window.parent === window.self) {
        max_width = window.document.documentElement.clientWidth;
        max_height = window.document.documentElement.clientHeight;
    } else {
        var panel = $(".tabs-panels", window.parent.document);

        max_width = panel.width;
        max_height = panel.height;
    }

    $('#listGrid').datagrid({
        fit: true,
        toolbar: '#toolbar',
        title: '品牌列表',
        iconCls: 'icon icon-chart_organisation',
        rownumbers: true,
        singleSelect: true,
        pagination: true,

        url: '/product/brand/list',
        queryParams: {
            _token: $('meta[name="csrf-token"]').attr('content'),
            curr_menu_id: $('meta[name="curr_menu_id"]').attr('content')
        },
        columns: [[
            {title: 'id', field: 'id', hidden: true},
            {title: '名称', field: 'name', width: 130},
            {title: '首字母', field: 'initial', width: 130},
            {title: '图片', field: 'pic', width: 130,align:'center',formatter: function(value,row,index){
                return '<img src="'+RESOURCE_URL+value+'" width="80" height="80">'
                }},
            {title: '是否推荐', field: 'is_recommend', width: 130}
        ]],
        onLoadSuccess: function (data) {
            if (data === null || data.total == 0) {
                var body = $(this).data().datagrid.dc.body2;
                body.find('table tbody').append('<tr><td width="' + body.width() + '" style="height: 35px; text-align: center;"><h1>暂无数据</h1></td></tr>');
            }
        }
    });
})

function openAdd() {
    $('#form_dialog').dialog({
        title: '添加信息',
        width: 400,
        height: 300,
        closed: false,
        cache: false,
        href: '/product/brand/add',
        modal: true,
        buttons: [{
            text: '确定',
            iconCls: 'icon-ok',
            handler: save
        }, {
            text: '取消',
            iconCls: 'icon-cancel',
            handler: function () {
                $('#form_dialog').dialog('close');
            }
        }]
    });
}

function openEdit() {
    var rows = $('#listGrid').datagrid('getSelected');
    if (null == rows) {
        $.messager.alert("提示信息", "请选择需要编辑的行", "info");
        return;
    }
    $('#form_dialog').dialog({
        title: '编辑信息',
        width: 400,
        height: 200,
        closed: false,
        cache: false,
        href: '/product/brand/edit?brand_id=' + rows.id,
        modal: true,
        buttons: [{
            text: '确定',
            iconCls: 'icon-ok',
            handler: save
        }, {
            text: '取消',
            iconCls: 'icon-cancel',
            handler: function () {
                $('#form_dialog').dialog('close');
            }
        }]
    });
}

function save() {

    //添加图片
    var form = new FormData();
    var dataURI = document.getElementById('Img').src;
    try {
        var byteString = atob(dataURI.split(',')[1]);
        var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
// 类型数组
        var ia = new Uint8Array(byteString.length);
        for (var i = 0; i < byteString.length; i++) {
            ia[i] = byteString.charCodeAt(i);
        }
        var blob = new Blob([ia], {
            type: mimeString
        });
        form.append('pic', blob);
    } catch (e) {
        //修改时可能不会修改上传图片
    }
    form.append('id', $.trim($('input[name=id]').val()));
    form.append('name', $.trim($('input[name=name]').val()));
    form.append('initial', $.trim($('input[name=initial]').val()));
    form.append('isRecommend', $('input[name=isRecommend]').val());
    $.ajax({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },
        url: '/product/brand/save',
        type: 'post',
        cache: false,
        processData: false,
        contentType: false,
        data: form,
        dataType: 'json',
        success: function (data) {
            if (data.code == 200) {
                $('#listGrid').datagrid("reload")
                $('#form_dialog').dialog('close');
            } else {
                $.messager.alert("提示信息", data.data, "error");
            }
        }
    });
}

function reload() {
    $('#listGrid').datagrid("reload");
}

function change_photo() {
    PreviewImage($("input[name='pic']")[0], 'Img', 'Imgdiv');
}
